package bootstrap

import (
	"devops-dcc/internal/controller"
	"devops-dcc/internal/middleware"
	"devops-dcc/internal/repositories"
	"devops-dcc/internal/services"
	"github.com/gin-gonic/gin"
)

func initRoutes() {
	router = gin.New()

	// 公共中间件
	router.Use(
		gin.Recovery(),
		middleware.LoggerMiddleware(),
		middleware.RateLimitMiddleware(100),
	)

	// 初始化控制器
	platformRepo := repositories.NewGormPlatformRepository(db)
	platformService := services.NewPlatformService(platformRepo)
	platformController := controller.NewPlatformController(platformService)

	// 注册路由
	registerHealthCheck()
	registerAPIRoutes(platformController, router)
}

func registerHealthCheck() {
	router.GET("/health", func(c *gin.Context) {
		c.JSON(200, gin.H{"status": "ok"})
	})
}

// @title 账号管理API
// @version 1.0

func registerAPIRoutes(ctrl *controller.PlatformController, router *gin.Engine) {
	api := router.Group("/api/v1")
	{
		// 平台管理
		platforms := api.Group("/platforms")
		{
			platforms.POST("", ctrl.CreatePlatform)
			platforms.GET("", ctrl.GetAllPlatforms)
			platforms.GET("/:id", ctrl.GetPlatform)
			platforms.PUT("/:id", ctrl.UpdatePlatform)
			platforms.DELETE("/:id", ctrl.DeletePlatform)
		}

		// 环境管理
		environments := api.Group("/environments")
		{
			environments.POST("", ctrl.CreateEnvironment)
			environments.GET("", ctrl.GetAllEnvironments)
		}

		// 配置管理
		configs := api.Group("/configurations")
		{
			configs.POST("/platform/:platform_id/environment/:environment_id", ctrl.SetPlatformEnvConfig)
			configs.GET("/platform/:platform_id/environment/:environment_id", ctrl.GetPlatformEnvConfig)
		}
	}
}
